package com.smile.commons.gps.example;

import com.smile.commons.gps.AreaCalculationUtil;
import com.smile.commons.gps.domain.CoordinatePointReq;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 面积计算工具使用示例
 * 演示如何计算多边形覆盖面积
 * 
 * @author System
 * @since 2025-10-14
 */
public class AreaCalculationExample {

    public static void main(String[] args) {
        System.out.println("=== 多边形面积计算工具示例 ===\n");

        // 示例1：计算矩形面积
        testRectangleArea();

        // 示例2：计算三角形面积
        testTriangleArea();

        // 示例3：计算五角星面积
        testStarShapeArea();

        // 示例4：计算实际区域面积
        testRealArea();
    }

    /**
     * 测试矩形面积计算
     */
    private static void testRectangleArea() {
        System.out.println("【示例1】计算矩形面积");
        System.out.println("----------------------------");

        // 创建一个矩形（约100米 x 100米）
        List<CoordinatePointReq> rectangle = new ArrayList<>();
        
        // 左下角
        rectangle.add(createPoint(30.250000, 120.140000));
        // 右下角
        rectangle.add(createPoint(30.250000, 120.141000));
        // 右上角
        rectangle.add(createPoint(30.251000, 120.141000));
        // 左上角
        rectangle.add(createPoint(30.251000, 120.140000));

        // 计算面积
        double area = AreaCalculationUtil.calculatePolygonArea(rectangle);
        AreaCalculationUtil.AreaResult result = AreaCalculationUtil.calculatePolygonAreaWithFormat(rectangle);
        double perimeter = AreaCalculationUtil.calculatePolygonPerimeter(rectangle);

        System.out.println("矩形顶点数: " + rectangle.size());
        System.out.println("面积: " + area + " 平方米");
        System.out.println("格式化结果: " + result);
        System.out.println("周长: " + String.format("%.2f", perimeter) + " 米");
        System.out.println();
    }

    /**
     * 测试三角形面积计算
     */
    private static void testTriangleArea() {
        System.out.println("【示例2】计算三角形面积");
        System.out.println("----------------------------");

        // 创建一个三角形
        List<CoordinatePointReq> triangle = new ArrayList<>();
        
        triangle.add(createPoint(30.250000, 120.140000));
        triangle.add(createPoint(30.250000, 120.141000));
        triangle.add(createPoint(30.251000, 120.140500));

        // 计算面积
        AreaCalculationUtil.AreaResult result = AreaCalculationUtil.calculatePolygonAreaWithFormat(triangle);
        double perimeter = AreaCalculationUtil.calculatePolygonPerimeter(triangle);

        System.out.println("三角形顶点数: " + triangle.size());
        System.out.println("格式化结果: " + result);
        System.out.println("周长: " + String.format("%.2f", perimeter) + " 米");
        System.out.println();
    }

    /**
     * 测试五角星面积计算
     */
    private static void testStarShapeArea() {
        System.out.println("【示例3】计算五角星面积");
        System.out.println("----------------------------");

        // 创建五角星形状
        List<CoordinatePointReq> star = createStarShape();

        // 计算面积
        AreaCalculationUtil.AreaResult result = AreaCalculationUtil.calculatePolygonAreaWithFormat(star);
        double perimeter = AreaCalculationUtil.calculatePolygonPerimeter(star);

        System.out.println("五角星顶点数: " + star.size());
        System.out.println("格式化结果: " + result);
        System.out.println("周长: " + String.format("%.2f", perimeter) + " 米");
        System.out.println();
    }

    /**
     * 测试实际区域面积计算
     */
    private static void testRealArea() {
        System.out.println("【示例4】计算实际区域面积（约1000米 x 1000米）");
        System.out.println("----------------------------");

        // 创建一个较大的矩形区域（约1000米 x 1000米）
        List<CoordinatePointReq> largeArea = new ArrayList<>();
        
        largeArea.add(createPoint(30.250000, 120.140000));
        largeArea.add(createPoint(30.250000, 120.150000));
        largeArea.add(createPoint(30.260000, 120.150000));
        largeArea.add(createPoint(30.260000, 120.140000));

        // 计算面积
        AreaCalculationUtil.AreaResult result = AreaCalculationUtil.calculatePolygonAreaWithFormat(largeArea);
        double perimeter = AreaCalculationUtil.calculatePolygonPerimeter(largeArea);

        System.out.println("区域顶点数: " + largeArea.size());
        System.out.println("格式化结果: " + result);
        System.out.println("周长: " + String.format("%.2f", perimeter) + " 米");
        
        // 详细显示各种单位
        System.out.println("\n详细面积信息:");
        System.out.println("  - 平方米: " + result.getSquareMetersFormatted() + " m²");
        System.out.println("  - 亩: " + result.getMuFormatted() + " 亩");
        System.out.println("  - 公顷: " + result.getHectaresFormatted() + " 公顷");
        System.out.println("  - 平方千米: " + result.getSquareKilometersFormatted() + " km²");
        System.out.println();
    }

    /**
     * 创建五角星形状
     */
    private static List<CoordinatePointReq> createStarShape() {
        List<CoordinatePointReq> star = new ArrayList<>();
        
        // 五角星参数
        double centerLat = 30.254000;
        double centerLon = 120.144000;
        double outerRadius = 0.002;  // 外半径约200米
        double innerRadius = 0.001;  // 内半径约100米
        
        // 生成10个顶点（5外+5内）
        for (int i = 0; i < 10; i++) {
            double angle = Math.toRadians(i * 36 - 90);
            double radius = (i % 2 == 0) ? outerRadius : innerRadius;
            
            double lat = centerLat + radius * Math.cos(angle);
            double lon = centerLon + radius * Math.sin(angle);
            
            star.add(createPoint(lat, lon));
        }
        
        return star;
    }

    /**
     * 创建坐标点
     */
    private static CoordinatePointReq createPoint(double lat, double lon) {
        CoordinatePointReq point = new CoordinatePointReq();
        point.setLatitude(new BigDecimal(lat));
        point.setLongitude(new BigDecimal(lon));
        point.setHeight(new BigDecimal("80"));
        return point;
    }
}
